package com.tracking.locon;

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class Main extends FragmentActivity implements ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;
    
    //Set the default tab when opening the app, 0 is the first tab
    private static final int DEFAULT_TAB = 2;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        
        // When swiping between different sections, select the corresponding tab.
        // We can also use ActionBar.Tab#select() to do this if we have a reference to the
        // Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                    		.setIcon(mSectionsPagerAdapter.getPageIcon(i))
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
        //Set the default tab
        mViewPager.setCurrentItem(DEFAULT_TAB);   
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig){
    	super.onConfigurationChanged(newConfig);
    	ActionBar actionBar = getActionBar();
    	mViewPager.setCurrentItem(actionBar.getSelectedNavigationIndex());
        /*
    	//If orientation is Landscape
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            for(int i = 0; i < actionBar.getTabCount(); i++){
            	actionBar.getTabAt(i).setText(mSectionsPagerAdapter.getPageTitle(i));
            }
        //If orientation is Portrait
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        	for(int i = 0; i < actionBar.getTabCount(); i++){
            	actionBar.getTabAt(i).setText("");
            }
        }
        */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
        	Fragment fragment = null;
        	switch(i){
        	case 0:
        		//fragment = new FragmentClass(Contact.class);
        		fragment = new Contact();
        		break;  
        	case 1:
        		//fragment = new FragmentClass(Chat.class);
        		fragment = new Chat();
        		break;
        	case 2:
        		//fragment = new FragmentClass(Track.class);
        		fragment = new FragmentTrack();
                break;
        	}
        	return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return getString(R.string.title_contact).toUpperCase(Locale.ENGLISH);
                case 1: return getString(R.string.title_chat).toUpperCase(Locale.ENGLISH);
                case 2: return getString(R.string.title_track).toUpperCase(Locale.ENGLISH);
            }
            return null;
        }
        
        public Drawable getPageIcon(int position){
        	switch (position) {
	            case 0: return getResources().getDrawable(R.drawable.icon_contact);
	            case 1: return getResources().getDrawable(R.drawable.icon_chat);
	            case 2: return getResources().getDrawable(R.drawable.icon_track);
        	}
        	return null;
        }
    }
}
